home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / compress / unzip531.zip / unix / unix.c < prev    next >
C/C++ Source or Header  |  1997-03-09  |  38KB  |  1,188 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unix.c
  4.  
  5.   Unix-specific routines for use with Info-ZIP's UnZip 5.3 and later.
  6.  
  7.   Contains:  readdir()
  8.              do_wild()           <-- generic enough to put in fileio.c?
  9.              mapattr()
  10.              mapname()
  11.              checkdir()
  12.              mkdir()
  13.              close_outfile()
  14.              version()
  15.  
  16.   ---------------------------------------------------------------------------*/
  17.  
  18.  
  19. #define UNZIP_INTERNAL
  20. #include "unzip.h"
  21.  
  22. #ifdef SCO_XENIX
  23. #  define SYSNDIR
  24. #else  /* SCO Unix, AIX, DNIX, TI SysV, Coherent 4.x, ... */
  25. #  if defined(__convexc__) || defined(SYSV) || defined(CRAY) || defined(BSD4_4)
  26. #    define DIRENT
  27. #  endif
  28. #endif
  29. #if defined(_AIX)
  30. #  define DIRENT
  31. #endif
  32. #ifdef COHERENT
  33. #  if defined(_I386) || (defined(__COHERENT__) && (__COHERENT__ >= 0x420))
  34. #    define DIRENT
  35. #  endif
  36. #endif
  37.  
  38. /* GRR:  may need to uncomment this: */
  39. #if 0
  40. #if defined(_POSIX_VERSION)
  41. #  define DIRENT
  42. #endif
  43. #endif
  44.  
  45. #ifdef DIRENT
  46. #  include <dirent.h>
  47. #else
  48. #  ifdef SYSV
  49. #    ifdef SYSNDIR
  50. #      include <sys/ndir.h>
  51. #    else
  52. #      include <ndir.h>
  53. #    endif
  54. #  else /* !SYSV */
  55. #    ifndef NO_SYSDIR
  56. #      include <sys/dir.h>
  57. #    endif
  58. #  endif /* ?SYSV */
  59. #  ifndef dirent
  60. #    define dirent direct
  61. #  endif
  62. #endif /* ?DIRENT */
  63.  
  64. static int created_dir;        /* used in mapname(), checkdir() */
  65. static int renamed_fullpath;   /* ditto */
  66.  
  67.  
  68. #ifndef SFX
  69. #ifdef NO_DIR                  /* for AT&T 3B1 */
  70.  
  71. #define opendir(path) fopen(path,"r")
  72. #define closedir(dir) fclose(dir)
  73. typedef FILE DIR;
  74.  
  75. /*
  76.  *  Apparently originally by Rich Salz.
  77.  *  Cleaned up and modified by James W. Birdsall.
  78.  */
  79. struct dirent *readdir(dirp)
  80.     DIR *dirp;
  81. {
  82.     static struct dirent entry;
  83.  
  84.     if (dirp == NULL)
  85.         return NULL;
  86.  
  87.     for (;;)
  88.         if (fread(&entry, sizeof (struct dirent), 1, dirp) == 0)
  89.             return (struct dirent *)NULL;
  90.         else if (entry.d_ino)
  91.             return &entry;
  92.  
  93. } /* end function readdir() */
  94.  
  95. #endif /* NO_DIR */
  96.  
  97.  
  98. /**********************/
  99. /* Function do_wild() */   /* for porting:  dir separator; match(ignore_case) */
  100. /**********************/
  101.  
  102. char *do_wild(__G__ wildspec)
  103.     __GDEF
  104.     char *wildspec;         /* only used first time on a given dir */
  105. {
  106.     static DIR *dir = (DIR *)NULL;
  107.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  108.     static int firstcall=TRUE, have_dirname, dirnamelen;
  109.     struct dirent *file;
  110.  
  111.  
  112.     /* Even when we're just returning wildspec, we *always* do so in
  113.      * matchname[]--calling routine is allowed to append four characters
  114.      * to the returned string, and wildspec may be a pointer to argv[].
  115.      */
  116.     if (firstcall) {        /* first call:  must initialize everything */
  117.         firstcall = FALSE;
  118.  
  119.         /* break the wildspec into a directory part and a wildcard filename */
  120.         if ((wildname = strrchr(wildspec, '/')) == (char *)NULL) {
  121.             dirname = ".";
  122.             dirnamelen = 1;
  123.             have_dirname = FALSE;
  124.             wildname = wildspec;
  125.         } else {
  126.             ++wildname;     /* point at character after '/' */
  127.             dirnamelen = wildname - wildspec;
  128.             if ((dirname = (char *)malloc(dirnamelen+1)) == (char *)NULL) {
  129.                 Info(slide, 0x201, ((char *)slide,
  130.                   "warning:  can't allocate wildcard buffers\n"));
  131.                 strcpy(matchname, wildspec);
  132.                 return matchname;   /* but maybe filespec was not a wildcard */
  133.             }
  134.             strncpy(dirname, wildspec, dirnamelen);
  135.             dirname[dirnamelen] = '\0';   /* terminate for strcpy below */
  136.             have_dirname = TRUE;
  137.         }
  138.  
  139.         if ((dir = opendir(dirname)) != (DIR *)NULL) {
  140.             while ((file = readdir(dir)) != (struct dirent *)NULL) {
  141.                 if (file->d_name[0] == '.' && wildname[0] != '.')
  142.                     continue;  /* Unix:  '*' and '?' do not match leading dot */
  143.                 if (match(file->d_name, wildname, 0)) {  /* 0 == case sens. */
  144.                     if (have_dirname) {
  145.                         strcpy(matchname, dirname);
  146.                         strcpy(matchname+dirnamelen, file->d_name);
  147.                     } else
  148.                         strcpy(matchname, file->d_name);
  149.                     return matchname;
  150.                 }
  151.             }
  152.             /* if we get to here directory is exhausted, so close it */
  153.             closedir(dir);
  154.             dir = (DIR *)NULL;
  155.         }
  156.  
  157.         /* return the raw wildspec in case that works (e.g., directory not
  158.          * searchable, but filespec was not wild and file is readable) */
  159.         strcpy(matchname, wildspec);
  160.         return matchname;
  161.     }
  162.  
  163.     /* last time through, might have failed opendir but returned raw wildspec */
  164.     if (dir == (DIR *)NULL) {
  165.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  166.         if (have_dirname)
  167.             free(dirname);
  168.         return (char *)NULL;
  169.     }
  170.  
  171.     /* If we've gotten this far, we've read and matched at least one entry
  172.      * successfully (in a previous call), so dirname has been copied into
  173.      * matchname already.
  174.      */
  175.     while ((file = readdir(dir)) != (struct dirent *)NULL)
  176.         if (match(file->d_name, wildname, 0)) {   /* 0 == don't ignore case */
  177.             if (have_dirname) {
  178.                 /* strcpy(matchname, dirname); */
  179.                 strcpy(matchname+dirnamelen, file->d_name);
  180.             } else
  181.                 strcpy(matchname, file->d_name);
  182.             return matchname;
  183.         }
  184.  
  185.     closedir(dir);     /* have read at least one dir entry; nothing left */
  186.     dir = (DIR *)NULL;
  187.     firstcall = TRUE;  /* reset for new wildspec */
  188.     if (have_dirname)
  189.         free(dirname);
  190.     return (char *)NULL;
  191.  
  192. } /* end function do_wild() */
  193.  
  194. #endif /* !SFX */
  195.  
  196.  
  197.  
  198.  
  199.  
  200. /**********************/
  201. /* Function mapattr() */
  202. /**********************/
  203.  
  204. int mapattr(__G)
  205.     __GDEF
  206. {
  207.     ulg tmp = G.crec.external_file_attributes;
  208.  
  209.     switch (G.pInfo->hostnum) {
  210.         case UNIX_:
  211.         case VMS_:
  212.             G.pInfo->file_attr = (unsigned)(tmp >> 16);
  213.             return 0;
  214.         case AMIGA_:
  215.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  216.             G.pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  217.             break;
  218.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  219.         case FS_FAT_:
  220.         case FS_HPFS_:
  221.         case FS_NTFS_:
  222.         case MAC_:
  223.         case ATARI_:             /* (used to set = 0666) */
  224.         case TOPS20_:
  225.         default:
  226.             /* read-only bit --> write perms; subdir bit --> dir exec bit */
  227.             tmp = !(tmp & 1) << 1  |  (tmp & 0x10) >> 4;
  228.             G.pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  229.             break;
  230.     } /* end switch (host-OS-created-by) */
  231.  
  232.     /* for originating systems with no concept of "group," "other," "system": */
  233.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  234.     G.pInfo->file_attr &= ~tmp;
  235.  
  236.     return 0;
  237.  
  238. } /* end function mapattr() */
  239.  
  240.  
  241.  
  242.  
  243.  
  244. /************************/
  245. /*  Function mapname()  */
  246. /************************/
  247.                              /* return 0 if no error, 1 if caution (filename */
  248. int mapname(__G__ renamed)   /*  truncated), 2 if warning (skip file because */
  249.     __GDEF                   /*  dir doesn't exist), 3 if error (skip file), */
  250.     int renamed;             /*  or 10 if out of memory (skip file) */
  251. {                            /*  [also IZ_VOL_LABEL, IZ_CREATED_DIR] */
  252.     char pathcomp[FILNAMSIZ];      /* path-component buffer */
  253.     char *pp, *cp=(char *)NULL;    /* character pointers */
  254.     char *lastsemi=(char *)NULL;   /* pointer to last semi-colon in pathcomp */
  255.     int quote = FALSE;             /* flags */
  256.     int error = 0;
  257.     register unsigned workch;      /* hold the character being tested */
  258.  
  259.  
  260. /*---------------------------------------------------------------------------
  261.     Initialize various pointers and counters and stuff.
  262.   ---------------------------------------------------------------------------*/
  263.  
  264.     if (G.pInfo->vollabel)
  265.         return IZ_VOL_LABEL;    /* can't set disk volume labels in Unix */
  266.  
  267.     /* can create path as long as not just freshening, or if user told us */
  268.     G.create_dirs = (!G.fflag || renamed);
  269.  
  270.     created_dir = FALSE;        /* not yet */
  271.  
  272.     /* user gave full pathname:  don't prepend rootpath */
  273.     renamed_fullpath = (renamed && (*G.filename == '/'));
  274.  
  275.     if (checkdir(__G__ (char *)NULL, INIT) == 10)
  276.         return 10;              /* initialize path buffer, unless no memory */
  277.  
  278.     *pathcomp = '\0';           /* initialize translation buffer */
  279.     pp = pathcomp;              /* point to translation buffer */
  280.     if (G.jflag)                /* junking directories */
  281.         cp = (char *)strrchr(G.filename, '/');
  282.     if (cp == (char *)NULL)     /* no '/' or not junking dirs */
  283.         cp = G.filename;        /* point to internal zipfile-member pathname */
  284.     else
  285.         ++cp;                   /* point to start of last component of path */
  286.  
  287. /*---------------------------------------------------------------------------
  288.     Begin main loop through characters in filename.
  289.   ---------------------------------------------------------------------------*/
  290.  
  291.     while ((workch = (uch)*cp++) != 0) {
  292.  
  293.         if (quote) {                 /* if character quoted, */
  294.             *pp++ = (char)workch;    /*  include it literally */
  295.             quote = FALSE;
  296.         } else
  297.             switch (workch) {
  298.             case '/':             /* can assume -j flag not given */
  299.                 *pp = '\0';
  300.                 if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  301.                     return error;
  302.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  303.                 lastsemi = (char *)NULL; /* leave directory semi-colons alone */
  304.                 break;
  305.  
  306.             case ';':             /* VMS version (or DEC-20 attrib?) */
  307.                 lastsemi = pp;
  308.                 *pp++ = ';';      /* keep for now; remove VMS ";##" */
  309.                 break;            /*  later, if requested */
  310.  
  311.             case '\026':          /* control-V quote for special chars */
  312.                 quote = TRUE;     /* set flag for next character */
  313.                 break;
  314.  
  315. #ifdef MTS
  316.             case ' ':             /* change spaces to underscore under */
  317.                 *pp++ = '_';      /*  MTS; leave as spaces under Unix */
  318.                 break;
  319. #endif
  320.  
  321.             default:
  322.                 /* allow European characters in filenames: */
  323.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  324.                     *pp++ = (char)workch;
  325.             } /* end switch */
  326.  
  327.     } /* end while loop */
  328.  
  329.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  330.  
  331.     /* if not saving them, remove VMS version numbers (appended ";###") */
  332.     if (!G.V_flag && lastsemi) {
  333.         pp = lastsemi + 1;
  334.         while (isdigit((uch)(*pp)))
  335.             ++pp;
  336.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  337.             *lastsemi = '\0';
  338.     }
  339.  
  340. /*---------------------------------------------------------------------------
  341.     Report if directory was created (and no file to create:  filename ended
  342.     in '/'), check name to be sure it exists, and combine path and name be-
  343.     fore exiting.
  344.   ---------------------------------------------------------------------------*/
  345.  
  346.     if (G.filename[strlen(G.filename) - 1] == '/') {
  347.         checkdir(__G__ G.filename, GETPATH);
  348.         if (created_dir) {
  349.             if (QCOND2) {
  350.                 Info(slide, 0, ((char *)slide, "   creating: %s\n",
  351.                   G.filename));
  352.             }
  353. #ifndef NO_CHMOD
  354.             /* set approx. dir perms (make sure can still read/write in dir) */
  355.             if (chmod(G.filename, (0xffff & G.pInfo->file_attr) | 0700))
  356.                 perror("chmod (directory attributes) error");
  357. #endif
  358.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  359.         }
  360.         return 2;   /* dir existed already; don't look for data to extract */
  361.     }
  362.  
  363.     if (*pathcomp == '\0') {
  364.         Info(slide, 1, ((char *)slide, "mapname:  conversion of %s failed\n",
  365.           G.filename));
  366.         return 3;
  367.     }
  368.  
  369.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  370.     checkdir(__G__ G.filename, GETPATH);
  371.  
  372.     return error;
  373.  
  374. } /* end function mapname() */
  375.  
  376.  
  377.  
  378.  
  379. #if 0  /*========== NOTES ==========*/
  380.  
  381.   extract-to dir:      a:path/
  382.   buildpath:           path1/path2/ ...   (NULL-terminated)
  383.   pathcomp:                filename
  384.  
  385.   mapname():
  386.     loop over chars in zipfile member name
  387.       checkdir(path component, COMPONENT | CREATEDIR) --> map as required?
  388.         (d:/tmp/unzip/)                    (disk:[tmp.unzip.)
  389.         (d:/tmp/unzip/jj/)                 (disk:[tmp.unzip.jj.)
  390.         (d:/tmp/unzip/jj/temp/)            (disk:[tmp.unzip.jj.temp.)
  391.     finally add filename itself and check for existence? (could use with rename)
  392.         (d:/tmp/unzip/jj/temp/msg.outdir)  (disk:[tmp.unzip.jj.temp]msg.outdir)
  393.     checkdir(name, GETPATH)     -->  copy path to name and free space
  394.  
  395. #endif /* 0 */
  396.  
  397.  
  398.  
  399.  
  400. /***********************/
  401. /* Function checkdir() */
  402. /***********************/
  403.  
  404. int checkdir(__G__ pathcomp, flag)
  405.     __GDEF
  406.     char *pathcomp;
  407.     int flag;
  408. /*
  409.  * returns:  1 - (on APPEND_NAME) truncated filename
  410.  *           2 - path doesn't exist, not allowed to create
  411.  *           3 - path doesn't exist, tried to create and failed; or
  412.  *               path exists and is not a directory, but is supposed to be
  413.  *           4 - path is too long
  414.  *          10 - can't allocate memory for filename buffers
  415.  */
  416. {
  417.     static int rootlen = 0;   /* length of rootpath */
  418.     static char *rootpath;    /* user's "extract-to" directory */
  419.     static char *buildpath;   /* full path (so far) to extracted file */
  420.     static char *end;         /* pointer to end of buildpath ('\0') */
  421.  
  422. #   define FN_MASK   7
  423. #   define FUNCTION  (flag & FN_MASK)
  424.  
  425.  
  426.  
  427. /*---------------------------------------------------------------------------
  428.     APPEND_DIR:  append the path component to the path being built and check
  429.     for its existence.  If doesn't exist and we are creating directories, do
  430.     so for this one; else signal success or error as appropriate.
  431.   ---------------------------------------------------------------------------*/
  432.  
  433.     if (FUNCTION == APPEND_DIR) {
  434.         int too_long = FALSE;
  435. #ifdef SHORT_NAMES
  436.         char *old_end = end;
  437. #endif
  438.  
  439.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  440.         while ((*end = *pathcomp++) != '\0')
  441.             ++end;
  442. #ifdef SHORT_NAMES   /* path components restricted to 14 chars, typically */
  443.         if ((end-old_end) > FILENAME_MAX)  /* GRR:  proper constant? */
  444.             *(end = old_end + FILENAME_MAX) = '\0';
  445. #endif
  446.  
  447.         /* GRR:  could do better check, see if overrunning buffer as we go:
  448.          * check end-buildpath after each append, set warning variable if
  449.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  450.          * appending.  Clear variable when begin new path. */
  451.  
  452.         if ((end-buildpath) > FILNAMSIZ-3)  /* need '/', one-char name, '\0' */
  453.             too_long = TRUE;                /* check if extracting directory? */
  454.         if (stat(buildpath, &G.statbuf)) {  /* path doesn't exist */
  455.             if (!G.create_dirs) { /* told not to create (freshening) */
  456.                 free(buildpath);
  457.                 return 2;         /* path doesn't exist:  nothing to do */
  458.             }
  459.             if (too_long) {
  460.                 Info(slide, 1, ((char *)slide,
  461.                   "checkdir error:  path too long: %s\n", buildpath));
  462.                 free(buildpath);
  463.                 return 4;         /* no room for filenames:  fatal */
  464.             }
  465.             if (mkdir(buildpath, 0777) == -1) {   /* create the directory */
  466.                 Info(slide, 1, ((char *)slide,
  467.                   "checkdir error:  can't create %s\n\
  468.                  unable to process %s.\n", buildpath, G.filename));
  469.                 free(buildpath);
  470.                 return 3;      /* path didn't exist, tried to create, failed */
  471.             }
  472.             created_dir = TRUE;
  473.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  474.             Info(slide, 1, ((char *)slide,
  475.               "checkdir error:  %s exists but is not directory\n\
  476.                  unable to process %s.\n", buildpath, G.filename));
  477.             free(buildpath);
  478.             return 3;          /* path existed but wasn't dir */
  479.         }
  480.         if (too_long) {
  481.             Info(slide, 1, ((char *)slide,
  482.               "checkdir error:  path too long: %s\n", buildpath));
  483.             free(buildpath);
  484.             return 4;         /* no room for filenames:  fatal */
  485.         }
  486.         *end++ = '/';
  487.         *end = '\0';
  488.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  489.         return 0;
  490.  
  491.     } /* end if (FUNCTION == APPEND_DIR) */
  492.  
  493. /*---------------------------------------------------------------------------
  494.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  495.     buildpath.
  496.   ---------------------------------------------------------------------------*/
  497.  
  498.     if (FUNCTION == GETPATH) {
  499.         strcpy(pathcomp, buildpath);
  500.         Trace((stderr, "getting and freeing path [%s]\n", pathcomp));
  501.         free(buildpath);
  502.         buildpath = end = (char *)NULL;
  503.         return 0;
  504.     }
  505.  
  506. /*---------------------------------------------------------------------------
  507.     APPEND_NAME:  assume the path component is the filename; append it and
  508.     return without checking for existence.
  509.   ---------------------------------------------------------------------------*/
  510.  
  511.     if (FUNCTION == APPEND_NAME) {
  512. #ifdef SHORT_NAMES
  513.         char *old_end = end;
  514. #endif
  515.  
  516.         Trace((stderr, "appending filename [%s]\n", pathcomp));
  517.         while ((*end = *pathcomp++) != '\0') {
  518.             ++end;
  519. #ifdef SHORT_NAMES  /* truncate name at 14 characters, typically */
  520.             if ((end-old_end) > FILENAME_MAX)      /* GRR:  proper constant? */
  521.                 *(end = old_end + FILENAME_MAX) = '\0';
  522. #endif
  523.             if ((end-buildpath) >= FILNAMSIZ) {
  524.                 *--end = '\0';
  525.                 Info(slide, 0x201, ((char *)slide,
  526.                   "checkdir warning:  path too long; truncating\n\
  527.                    %s\n                -> %s\n", G.filename, buildpath));
  528.                 return 1;   /* filename truncated */
  529.             }
  530.         }
  531.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  532.         return 0;  /* could check for existence here, prompt for new name... */
  533.     }
  534.  
  535. /*---------------------------------------------------------------------------
  536.     INIT:  allocate and initialize buffer space for the file currently being
  537.     extracted.  If file was renamed with an absolute path, don't prepend the
  538.     extract-to path.
  539.   ---------------------------------------------------------------------------*/
  540.  
  541. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  542.  
  543.     if (FUNCTION == INIT) {
  544.         Trace((stderr, "initializing buildpath to "));
  545.         if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+1)) ==
  546.             (char *)NULL)
  547.             return 10;
  548.         if ((rootlen > 0) && !renamed_fullpath) {
  549.             strcpy(buildpath, rootpath);
  550.             end = buildpath + rootlen;
  551.         } else {
  552.             *buildpath = '\0';
  553.             end = buildpath;
  554.         }
  555.         Trace((stderr, "[%s]\n", buildpath));
  556.         return 0;
  557.     }
  558.  
  559. /*---------------------------------------------------------------------------
  560.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  561.     sary; else assume it's a zipfile member and return.  This path segment
  562.     gets used in extracting all members from every zipfile specified on the
  563.     command line.
  564.   ---------------------------------------------------------------------------*/
  565.  
  566. #if (!defined(SFX) || defined(SFX_EXDIR))
  567.     if (FUNCTION == ROOT) {
  568.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  569.         if (pathcomp == (char *)NULL) {
  570.             rootlen = 0;
  571.             return 0;
  572.         }
  573.         if ((rootlen = strlen(pathcomp)) > 0) {
  574.             if (pathcomp[rootlen-1] == '/') {
  575.                 pathcomp[--rootlen] = '\0';
  576.             }
  577.             if (rootlen > 0 && (stat(pathcomp, &G.statbuf) ||
  578.                 !S_ISDIR(G.statbuf.st_mode)))        /* path does not exist */
  579.             {
  580.                 if (!G.create_dirs /* || iswild(pathcomp) */ ) {
  581.                     rootlen = 0;
  582.                     return 2;   /* skip (or treat as stored file) */
  583.                 }
  584.                 /* create the directory (could add loop here to scan pathcomp
  585.                  * and create more than one level, but why really necessary?) */
  586.                 if (mkdir(pathcomp, 0777) == -1) {
  587.                     Info(slide, 1, ((char *)slide,
  588.                       "checkdir:  can't create extraction directory: %s\n",
  589.                       pathcomp));
  590.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  591.                     return 3;  /* failed:  file exists, or 2+ levels required */
  592.                 }
  593.             }
  594.             if ((rootpath = (char *)malloc(rootlen+2)) == (char *)NULL) {
  595.                 rootlen = 0;
  596.                 return 10;
  597.             }
  598.             strcpy(rootpath, pathcomp);
  599.             rootpath[rootlen++] = '/';
  600.             rootpath[rootlen] = '\0';
  601.             Trace((stderr, "rootpath now = [%s]\n", rootpath));
  602.         }
  603.         return 0;
  604.     }
  605. #endif /* !SFX || SFX_EXDIR */
  606.  
  607. /*---------------------------------------------------------------------------
  608.     END:  free rootpath, immediately prior to program exit.
  609.   ---------------------------------------------------------------------------*/
  610.  
  611.     if (FUNCTION == END) {
  612.         Trace((stderr, "freeing rootpath\n"));
  613.         if (rootlen > 0)
  614.             free(rootpath);
  615.         return 0;
  616.     }
  617.  
  618.     return 99;  /* should never reach */
  619.  
  620. } /* end function checkdir() */
  621.  
  622.  
  623.  
  624.  
  625.  
  626. #ifdef NO_MKDIR
  627.  
  628. /********************/
  629. /* Function mkdir() */
  630. /********************/
  631.  
  632. int mkdir(path, mode)
  633.     char *path;
  634.     int mode;   /* ignored */
  635. /*
  636.  * returns:   0 - successful
  637.  *           -1 - failed (errno not set, however)
  638.  */
  639. {
  640.     char command[FILNAMSIZ+40]; /* buffer for system() call */
  641.  
  642.     /* GRR 930416:  added single quotes around path to avoid bug with
  643.      * creating directories with ampersands in name; not yet tested */
  644.     sprintf(command, "IFS=\" \t\n\" /bin/mkdir '%s' 2>/dev/null", path);
  645.     if (system(command))
  646.         return -1;
  647.     return 0;
  648. }
  649.  
  650. #endif /* NO_MKDIR */
  651.  
  652.  
  653.  
  654.  
  655.  
  656. #if 0
  657. #ifdef MORE
  658.  
  659. /**************************/
  660. /* Function screenlines() */
  661. /**************************/
  662.  
  663. int screenlines()
  664. {
  665.     char *envptr, *getenv();
  666.     int n;
  667.  
  668.     /* GRR:  this is overly simplistic; should use winsize struct and
  669.      * appropriate TIOCGWINSZ ioctl(), assuming exists on enough systems
  670.      */
  671.     envptr = getenv("LINES");
  672.     if (envptr == (char *)NULL || (n = atoi(envptr)) < 5)
  673.         return 24;   /* VT-100 assumed to be minimal hardware */
  674.     else
  675.         return n;
  676. }
  677.  
  678. #endif /* MORE */
  679. #endif /* 0 */
  680.  
  681.  
  682.  
  683.  
  684.  
  685. #ifndef MTS
  686.  
  687. /****************************/
  688. /* Function close_outfile() */
  689. /****************************/
  690.  
  691. void close_outfile(__G)    /* GRR: change to return PK-style warning level */
  692.     __GDEF
  693. {
  694.     iztimes zt;
  695.     ush z_uidgid[2];
  696.     unsigned eb_izux_flg;
  697.  
  698. /*---------------------------------------------------------------------------
  699.     If symbolic links are supported, allocate a storage area, put the uncom-
  700.     pressed "data" in it, and create the link.  Since we know it's a symbolic
  701.     link to start with, we shouldn't have to worry about overflowing unsigned
  702.     ints with unsigned longs.
  703.   ---------------------------------------------------------------------------*/
  704.  
  705. #ifdef SYMLINKS
  706.     if (G.symlnk) {
  707.         unsigned ucsize = (unsigned)G.lrec.ucsize;
  708.         char *linktarget = (char *)malloc((unsigned)G.lrec.ucsize+1);
  709.  
  710.         fclose(G.outfile);                      /* close "data" file... */
  711.         G.outfile = fopen(G.filename, FOPR);    /* ...and reopen for reading */
  712.         if (!linktarget || fread(linktarget, 1, ucsize, G.outfile) !=
  713.                            (int)ucsize)
  714.         {
  715.             Info(slide, 0x201, ((char *)slide,
  716.               "warning:  symbolic link (%s) failed\n", G.filename));
  717.             if (linktarget)
  718.                 free(linktarget);
  719.             fclose(G.outfile);
  720.             return;
  721.         }
  722.         fclose(G.outfile);                  /* close "data" file for good... */
  723.         unlink(G.filename);                 /* ...and delete it */
  724.         linktarget[ucsize] = '\0';
  725.         Info(slide, 0, ((char *)slide, "-> %s ", linktarget));
  726.         if (symlink(linktarget, G.filename))  /* create the real link */
  727.             perror("symlink error");
  728.         free(linktarget);
  729.         return;                             /* can't set time on symlinks */
  730.     }
  731. #endif /* SYMLINKS */
  732.  
  733.     fclose(G.outfile);
  734. #ifdef QLZIP
  735.     if (G.extra_field) {
  736.         static void qlfix OF((__GPRO__ uch *ef_ptr, unsigned ef_len));
  737.  
  738.         qlfix(__G__ G.extra_field, G.lrec.extra_field_length);
  739.     }
  740. #endif
  741.  
  742. /*---------------------------------------------------------------------------
  743.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  744.     time:  adjust base year from 1980 to 1970, do usual conversions from
  745.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  746.     light savings time differences.  If we have a Unix extra field, however,
  747.     we're laughing:  both mtime and atime are ours.  On the other hand, we
  748.     then have to check for restoration of UID/GID.
  749.   ---------------------------------------------------------------------------*/
  750.  
  751.     eb_izux_flg = (G.extra_field ?
  752.                    ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length,
  753.                                     0, &zt, z_uidgid) : 0);
  754.     if (eb_izux_flg & EB_UT_FL_MTIME) {
  755.         TTrace((stderr, "\nclose_outfile:  Unix e.f. modif. time = %ld\n",
  756.           zt.mtime));
  757.     } else {
  758.         zt.mtime = dos_to_unix_time(G.lrec.last_mod_file_date,
  759.                                     G.lrec.last_mod_file_time);
  760.     }
  761.     if (eb_izux_flg & EB_UT_FL_ATIME) {
  762.         TTrace((stderr, "close_outfile:  Unix e.f. access time = %ld\n",
  763.           zt.atime));
  764.     } else {
  765.         zt.atime = zt.mtime;
  766.         TTrace((stderr, "\nclose_outfile:  modification/access times = %ld\n",
  767.           zt.mtime));
  768.     }
  769.  
  770.     /* if -X option was specified and we have UID/GID info, restore it */
  771.     if (G.X_flag && eb_izux_flg & EB_UX2_VALID) {
  772.         TTrace((stderr, "close_outfile:  restoring Unix UID/GID info\n"));
  773.         if (chown(G.filename, (uid_t)z_uidgid[0], (gid_t)z_uidgid[1]))
  774.         {
  775.             if (G.qflag)
  776.                 Info(slide, 0x201, ((char *)slide,
  777.                   "warning:  can't set UID %d and/or GID %d for %s\n",
  778.                   z_uidgid[0], z_uidgid[1], G.filename));
  779.             else
  780.                 Info(slide, 0x201, ((char *)slide,
  781.                   " (warning) can't set UID %d and/or GID %d",
  782.                   z_uidgid[0], z_uidgid[1]));
  783. /* GRR: change return type to int and set up to return warning after utime() */
  784.         }
  785.     }
  786.  
  787.     /* set the file's access and modification times */
  788.     if (utime(G.filename, (ztimbuf *)&zt))
  789. #ifdef AOS_VS
  790.         if (G.qflag)
  791.             Info(slide, 0x201, ((char *)slide, "... can't set time for %s\n",
  792.               G.filename));
  793.         else
  794.             Info(slide, 0x201, ((char *)slide, "... can't set time"));
  795. #else
  796.         if (G.qflag)
  797.             Info(slide, 0x201, ((char *)slide,
  798.               "warning:  can't set times for %s\n", G.filename));
  799.         else
  800.             Info(slide, 0x201, ((char *)slide,
  801.               " (warning) can't set times"));
  802. #endif /* ?AOS_VS */
  803.  
  804. /*---------------------------------------------------------------------------
  805.     Change the file permissions from default ones to those stored in the
  806.     zipfile.
  807.   ---------------------------------------------------------------------------*/
  808.  
  809. #ifndef NO_CHMOD
  810.     if (chmod(G.filename, 0xffff & G.pInfo->file_attr))
  811.         perror("chmod (file attributes) error");
  812. #endif
  813.  
  814. } /* end function close_outfile() */
  815.  
  816. #endif /* !MTS */
  817.  
  818.  
  819.  
  820.  
  821. #ifndef SFX
  822.  
  823. /************************/
  824. /*  Function version()  */
  825. /************************/
  826.  
  827. void version(__G)
  828.     __GDEF
  829. {
  830. #if defined(CRAY) || defined(NX_CURRENT_COMPILER_RELEASE) || defined(NetBSD)
  831.     char buf1[40];
  832. #if defined(CRAY) || defined(NX_CURRENT_COMPILER_RELEASE)
  833.     char buf2[40];
  834. #endif
  835. #endif
  836.  
  837.     /* Pyramid, NeXT have problems with huge macro expansion, too:  no Info() */
  838.     sprintf((char *)slide, LoadFarString(CompiledWith),
  839.  
  840. #ifdef __GNUC__
  841. #  ifdef NX_CURRENT_COMPILER_RELEASE
  842.       (sprintf(buf1, "NeXT DevKit %d.%02d ", NX_CURRENT_COMPILER_RELEASE/100,
  843.         NX_CURRENT_COMPILER_RELEASE%100), buf1),
  844.       (strlen(__VERSION__) > 8)? "(gcc)" :
  845.         (sprintf(buf2, "(gcc %s)", __VERSION__), buf2),
  846. #  else
  847.       "gcc ", __VERSION__,
  848. #  endif
  849. #else
  850. #  if defined(CRAY) && defined(_RELEASE)
  851.       "cc ", (sprintf(buf1, "version %d", _RELEASE), buf1),
  852. #  else
  853. #  ifdef __VERSION__
  854.       "cc ", __VERSION__,
  855. #  else
  856.       "cc", "",
  857. #  endif
  858. #  endif
  859. #endif
  860.  
  861.       "Unix",
  862.  
  863. #if defined(sgi) || defined(__sgi)
  864.       " (Silicon Graphics IRIX)",
  865. #else
  866. #ifdef sun
  867. #  ifdef sparc
  868. #    ifdef __SVR4
  869.       " (Sun SPARC/Solaris)",
  870. #    else /* may or may not be SunOS */
  871.       " (Sun SPARC)",
  872. #    endif
  873. #  else
  874. #  if defined(sun386) || defined(i386)
  875.       " (Sun 386i)",
  876. #  else
  877. #  if defined(mc68020) || defined(__mc68020__)
  878.       " (Sun 3)",
  879. #  else /* mc68010 or mc68000:  Sun 2 or earlier */
  880.       " (Sun 2)",
  881. #  endif
  882. #  endif
  883. #  endif
  884. #else
  885. #ifdef __hpux
  886.       " (HP/UX)",
  887. #else
  888. #ifdef __osf__
  889.       " (DEC OSF/1)",
  890. #else
  891. #ifdef _AIX
  892.       " (IBM AIX)",
  893. #else
  894. #ifdef aiws
  895.       " (IBM RT/AIX)",
  896. #else
  897. #if defined(CRAY) || defined(cray)
  898. #  ifdef _UNICOS
  899.       (sprintf(buf2, " (Cray UNICOS release %d)", _UNICOS), buf2),
  900. #  else
  901.       " (Cray UNICOS)",
  902. #  endif
  903. #else
  904. #if defined(uts) || defined(UTS)
  905.       " (Amdahl UTS)",
  906. #else
  907. #ifdef NeXT
  908. #  ifdef mc68000
  909.       " (NeXTStep/black)",
  910. #  else
  911.       " (NeXTStep for Intel)",
  912. #  endif
  913. #else              /* the next dozen or so are somewhat order-dependent */
  914. #ifdef LINUX
  915. #  ifdef __ELF__
  916.       " (Linux ELF)",
  917. #  else
  918.       " (Linux a.out)",
  919. #  endif
  920. #else
  921. #ifdef MINIX
  922.       " (Minix)",
  923. #else
  924. #ifdef M_UNIX
  925.       " (SCO Unix)",
  926. #else
  927. #ifdef M_XENIX
  928.       " (SCO Xenix)",
  929. #else
  930. #ifdef __NetBSD__
  931. #  ifdef NetBSD0_8
  932.       (sprintf(buf1, " (NetBSD 0.8%c)", (char)(NetBSD0_8 - 1 + 'A')), buf1),
  933. #  else
  934. #  ifdef NetBSD0_9
  935.       (sprintf(buf1, " (NetBSD 0.9%c)", (char)(NetBSD0_9 - 1 + 'A')), buf1),
  936. #  else
  937. #  ifdef NetBSD1_0
  938.       (sprintf(buf1, " (NetBSD 1.0%c)", (char)(NetBSD1_0 - 1 + 'A')), buf1),
  939. #  else
  940.       (BSD4_4 == 0.5)? " (NetBSD before 0.9)" : " (NetBSD 1.1 or later)",
  941. #  endif
  942. #  endif
  943. #  endif
  944. #else
  945. #ifdef __FreeBSD__
  946.       (BSD4_4 == 0.5)? " (FreeBSD 1.x)" : " (FreeBSD 2.0 or later)",
  947. #else
  948. #ifdef __bsdi__
  949.       (BSD4_4 == 0.5)? " (BSD/386 1.0)" : " (BSD/386 1.1 or later)",
  950. #else
  951. #ifdef __386BSD__
  952.       (BSD4_4 == 1)? " (386BSD, post-4.4 release)" : " (386BSD)",
  953. #else
  954. #if defined(i486) || defined(__i486) || defined(__i486__)
  955.       " (Intel 486)",
  956. #else
  957. #if defined(i386) || defined(__i386) || defined(__i386__)
  958.       " (Intel 386)",
  959. #else
  960. #ifdef pyr
  961.       " (Pyramid)",
  962. #else
  963. #ifdef ultrix
  964. #  ifdef mips
  965.       " (DEC/MIPS)",
  966. #  else
  967. #  ifdef vax
  968.       " (DEC/VAX)",
  969. #  else /* __alpha? */
  970.       " (DEC/Alpha)",
  971. #  endif
  972. #  endif
  973. #else
  974. #ifdef gould
  975.       " (Gould)",
  976. #else
  977. #ifdef MTS
  978.       " (MTS)",
  979. #else
  980. #ifdef __convexc__
  981.       " (Convex)",
  982. #else
  983. #ifdef __QNX__
  984.       " (QNX 4)",
  985. #else
  986. #ifdef __QNXNTO__
  987.       " (QNX Neutrino)",
  988. #else      
  989.       "",
  990. #endif /* QNX Neutrino */
  991. #endif /* QNX 4 */
  992. #endif /* Convex */
  993. #endif /* MTS */
  994. #endif /* Gould */
  995. #endif /* DEC */
  996. #endif /* Pyramid */
  997. #endif /* 386 */
  998. #endif /* 486 */
  999. #endif /* 386BSD */
  1000. #endif /* BSDI BSD/386 */
  1001. #endif /* NetBSD */
  1002. #endif /* FreeBSD */
  1003. #endif /* SCO Xenix */
  1004. #endif /* SCO Unix */
  1005. #endif /* Minix */
  1006. #endif /* Linux */
  1007. #endif /* NeXT */
  1008. #endif /* Amdahl */
  1009. #endif /* Cray */
  1010. #endif /* RT/AIX */
  1011. #endif /* AIX */
  1012. #endif /* OSF/1 */
  1013. #endif /* HP/UX */
  1014. #endif /* Sun */
  1015. #endif /* SGI */
  1016.  
  1017. #ifdef __DATE__
  1018.       " on ", __DATE__
  1019. #else
  1020.       "", ""
  1021. #endif
  1022.     );
  1023.  
  1024.     (*G.message)((zvoid *)&G, slide, (ulg)strlen((char *)slide), 0);
  1025.  
  1026. } /* end function version() */
  1027.  
  1028. #endif /* !SFX */
  1029.  
  1030.  
  1031.  
  1032.  
  1033. #ifdef QLZIP
  1034.  
  1035. struct qdirect  {
  1036.     long            d_length __attribute__ ((packed));  /* file length */
  1037.     unsigned char   d_access __attribute__ ((packed));  /* file access type */
  1038.     unsigned char   d_type __attribute__ ((packed));    /* file type */
  1039.     long            d_datalen __attribute__ ((packed)); /* data length */
  1040.     long            d_reserved __attribute__ ((packed));/* Unused */
  1041.     short           d_szname __attribute__ ((packed));  /* size of name */
  1042.     char            d_name[36] __attribute__ ((packed));/* name area */
  1043.     long            d_update __attribute__ ((packed));  /* last update */
  1044.     long            d_refdate __attribute__ ((packed));
  1045.     long            d_backup __attribute__ ((packed));   /* EOD */
  1046. };
  1047.  
  1048. #define LONGID  "QDOS02"
  1049. #define EXTRALEN (sizeof(struct qdirect) + 8)
  1050. #define JBLONGID    "QZHD"
  1051. #define JBEXTRALEN  (sizeof(jbextra)  - 4 * sizeof(char))
  1052.  
  1053. typedef struct {
  1054.     char        eb_header[4] __attribute__ ((packed));  /* place_holder */
  1055.     char        longid[8] __attribute__ ((packed));
  1056.     struct      qdirect     header __attribute__ ((packed));
  1057. } qdosextra;
  1058.  
  1059. typedef struct {
  1060.     char        eb_header[4];                           /* place_holder */
  1061.     char        longid[4];
  1062.     struct      qdirect     header;
  1063. } jbextra;
  1064.  
  1065.  
  1066.  
  1067. /*  The following two functions SH() and LG() convert big-endian short
  1068.  *  and long numbers into native byte order.  They are some kind of
  1069.  *  counterpart to the generic UnZip's makeword() and makelong() functions.
  1070.  */
  1071. static ush SH(ush val)
  1072. {
  1073.     uch swapbuf[2];
  1074.  
  1075.     swapbuf[1] = (uch)(val & 0xff);
  1076.     swapbuf[0] = (uch)(val >> 8);
  1077.     return (*(ush *)swapbuf);
  1078. }
  1079.  
  1080.  
  1081.  
  1082. static ulg LG(ulg val)
  1083. {
  1084.     /*  convert the big-endian unsigned long number `val' to the machine
  1085.      *  dependant representation
  1086.      */
  1087.     ush swapbuf[2];
  1088.  
  1089.     swapbuf[1] = SH((ush)(val & 0xffff));
  1090.     swapbuf[0] = SH((ush)(val >> 16));
  1091.     return (*(ulg *)swapbuf);
  1092. }
  1093.  
  1094.  
  1095.  
  1096. static void qlfix(__G__ ef_ptr, ef_len)
  1097.     __GDEF
  1098.     uch *ef_ptr;
  1099.     unsigned ef_len;
  1100. {
  1101.     while (ef_len >= EB_HEADSIZE)
  1102.     {
  1103.         unsigned    eb_id  = makeword(EB_ID + ef_ptr);
  1104.         unsigned    eb_len = makeword(EB_LEN + ef_ptr);
  1105.  
  1106.         if (eb_len > (ef_len - EB_HEADSIZE)) {
  1107.             /* discovered some extra field inconsistency! */
  1108.             Trace((stderr,
  1109.               "qlfix: block length %u > rest ef_size %u\n", eb_len,
  1110.               ef_len - EB_HEADSIZE));
  1111.             break;
  1112.         }
  1113.  
  1114.         switch (eb_id) {
  1115.           case EF_QDOS:
  1116.           {
  1117.             struct _ntc_
  1118.             {
  1119.                 long id;
  1120.                 long dlen;
  1121.             } ntc;
  1122.             long dlen = 0;
  1123.  
  1124.             qdosextra   *extra = (qdosextra *)ef_ptr;
  1125.             jbextra     *jbp   = (jbextra   *)ef_ptr;
  1126.  
  1127.             if (!strncmp(extra->longid, LONGID, strlen(LONGID)))
  1128.             {
  1129.                 if (eb_len != EXTRALEN)
  1130.                     if (G.qflag)
  1131.                         Info(slide, 0x201, ((char *)slide,
  1132.                           "warning:  invalid length in Qdos field for %s\n",
  1133.                           G.filename));
  1134.                     else
  1135.                         Info(slide, 0x201, ((char *)slide,
  1136.                           "warning:  invalid length in Qdos field"));
  1137.  
  1138.                 if (extra->header.d_type)
  1139.                 {
  1140.                     dlen = extra->header.d_datalen;
  1141.                 }
  1142.             }
  1143.  
  1144.             if (!strncmp(jbp->longid, JBLONGID, strlen(JBLONGID)))
  1145.             {
  1146.                 if (eb_len != JBEXTRALEN)
  1147.                     if (G.qflag)
  1148.                         Info(slide, 0x201, ((char *)slide,
  1149.                           "warning:  invalid length in QZ field for %s\n",
  1150.                           G.filename));
  1151.                     else
  1152.                         Info(slide, 0x201, ((char *)slide,
  1153.                           "warning:  invalid length in QZ field"));
  1154.                 if(jbp->header.d_type)
  1155.                 {
  1156.                     dlen = jbp->header.d_datalen;
  1157.                 }
  1158.             }
  1159.  
  1160.             if ((long)LG(dlen) > 0)
  1161.             {
  1162.                 G.outfile = fopen(G.filename,"r+");
  1163.                 fseek(G.outfile, -8, SEEK_END);
  1164.                 fread(&ntc, 8, 1, G.outfile);
  1165.                 if(ntc.id != *(long *)"XTcc")
  1166.                 {
  1167.                     ntc.id = *(long *)"XTcc";
  1168.                     ntc.dlen = dlen;
  1169.                     fwrite (&ntc, 8, 1, G.outfile);
  1170.                 }
  1171.                 Info(slide, 0x201, ((char *)slide, "QData = %d", LG(dlen)));
  1172.                 fclose(G.outfile);
  1173.             }
  1174.             return;     /* finished, cancel further extra field scanning */
  1175.           }
  1176.  
  1177.           default:
  1178.             Trace((stderr,"qlfix: unknown extra field block, ID=%d\n",
  1179.                eb_id));
  1180.         }
  1181.  
  1182.         /* Skip this extra field block */
  1183.         ef_ptr += (eb_len + EB_HEADSIZE);
  1184.         ef_len -= (eb_len + EB_HEADSIZE);
  1185.     }
  1186. }
  1187. #endif /* QLZIP */
  1188.